package h.c.z.designPattern.C02_SingletonPattern;

/**
 * 单例模式-懒汉式，双重检查（三）[推荐用]
 * @author jinsicao
 */
public class Singleton_02_04 {

	private static volatile Singleton_02_04 instance = null;
	
	private static int i = 0;
	
	private static int j = 0;
	
	private Singleton_02_04() {
		System.out.println("Singleton_02_04#" + i++);
	}
	
	/**
	 * 双检锁/双重校验锁（DCL，即 double-checked locking）
	 * @return
	 */
	public static Singleton_02_04 getInstance() {
		if (instance == null) {
			System.out.println("Singleton_02_04->getInstance#" + j++);
			synchronized (Singleton_02_04.class) {
				if (instance == null) {
					instance = new Singleton_02_04();
				}
			}
		}
		return instance;
	}
}
